//
//  HZGoodsDetailPicHeadView.m
//  app
//
//  Created by 刘华舟 on 15/5/26.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HZGoodsDetailPicHeadView.h"
#import "HZImageSourceConstant.h"

#import "UILabel+Conveniently.h"

@interface HZGoodsDetailPicHeadView()



@property (strong, nonatomic) HZPicsCarouselView *picCarslView;
@property (strong, nonatomic) UILabel *praceLabel;
@property (strong, nonatomic) UILabel *titleLabel;
@property (strong, nonatomic) UILabel *netPayLabel;

@property (strong, nonatomic) UILabel *moreInfoLabel;


@end

@implementation HZGoodsDetailPicHeadView

- (void)drawRect:(CGRect)rect{
    
    [self drawBackground:self.bounds color:HZ_MALL_MAIN_THEME_WHITE];
    
    [self drawHorizonLineLeft:[self.ubo uw:0] right:[self.ubo uw:0] at:[self.ubo fh:UB_BOTTOM ph:-floor(0.5)] color:HZ_MALL_M_SEPARATOR_CELL_LINE];
}


- (instancetype)initWithFrame:(CGRect)frame{
    
    frame = CGRectOf(frame.origin, [UBO_SCREEN fw:UB_WIDTH uh:kPicHeadViewHeight]);
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = HZ_MALL_MAIN_THEME_WHITE;
        self.contentMode = UIViewContentModeRedraw;
        //轮播图
        _picCarslView = [[HZPicsCarouselView alloc] initWithFrame:CGRectOf([self.ubo fx:UB_LEFT fy:UB_TOP],
                                                                           [self.ubo fw:UB_WIDTH fh:UB_HEIGHT uh:-(160+kSizeMallViewMargin)])];
        _picCarslView.pageTappedCaller = [UBTargetCaller of:self action:@selector(scrollPicsOnClick:)];
        [self addSubview:_picCarslView];
        
        //标题
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.picCarslView.ufo fx:UB_LEFT ux:kSizeMallViewMargin fy:UB_BOTTOM],
                                                              [self.ubo fw:UB_WIDTH uw:-2*kSizeMallViewMargin-kHZGoodsDetailPicHeadViewCommonLabelHeight uh:kHZGoodsDetailPicHeadViewCommonLabelHeight])];
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.textColor = HZ_MALL_TEXT_BLACK_COLOR;
        _titleLabel.font = kHZFont_Content_L;
        _titleLabel.numberOfLines = 2;
        [self addSubview:_titleLabel];
        
        
        //价格
        _praceLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.titleLabel.ufo fx:UB_LEFT fy:UB_BOTTOM],
                                                              [self.titleLabel.ubo uw:kHZGoodsDetailPicHeadViewPriceLabelWidth uh:kHZGoodsDetailPicHeadViewCommonLabelHeight])];
        _praceLabel.textAlignment = NSTextAlignmentLeft;
        _praceLabel.textColor = HZ_RED_COLOR;
        _praceLabel.font = kHZRFont_Menu_L(1.1, 1.1);
        [self addSubview:_praceLabel];
        
        //活动视图
        _netPayLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.praceLabel.ufo fx:UB_RIGHT ox:kSizeMallViewMargin/4 fy:UB_TOP oy:(_praceLabel.frame.size.height-kHZGoodsDetailPicHeadViewNetPayLabelHeight)/2],
                                                                 [self.ubo ow:kHZGoodsDetailPicHeadViewNetPayLabelWidth oh:kHZGoodsDetailPicHeadViewNetPayLabelHeight])];
        [self addSubview:_netPayLabel];

    
        
        
        //灰色标题
        _moreInfoLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.titleLabel.ufo fx:UB_LEFT fy:UB_BOTTOM],
                                                              [self.ubo fw:UB_WIDTH uw:-2*kSizeMallViewMargin uh:kHZGoodsDetailPicHeadViewCommonLabelHeight])];
        _moreInfoLabel.textAlignment = NSTextAlignmentLeft;
        _moreInfoLabel.textColor = HZ_MALL_TEXT_M_LIGHTGRAY_COLOR;
        _moreInfoLabel.font = kHZFont_Tag_M;
        _moreInfoLabel.numberOfLines = 0;
//        [self addSubview:_moreInfoLabel];
        
    }
    return self;
}

#pragma mark 点击查看图片
- (void)scrollPicsOnClick:(NSDictionary *)pictures{
    if (self.picsTargetCaller) {
        [self.picsTargetCaller call:pictures];
    }
}



//向视图中填充数据
- (void)fillViewWithModel:(HZGoodsDetailTopModel *)model{
    _model = model;
    
    self.frame = CGRectOf([UBO_WINDOW fx:UB_LEFT fy:UB_TOP], [UBO_SCREEN fw:UB_WIDTH oh:model.viewHeight]);
    
    /** 先调整视图位置 -write by khzliu */
    self.picCarslView.frame = CGRectOf([self.ubo fx:UB_LEFT fy:UB_TOP],
                                       [self.ubo fw:UB_WIDTH oh:model.topPictureHeight]);

    //标题
    _titleLabel.frame = CGRectOf([self.picCarslView.ufo fx:UB_LEFT ux:kSizeMallViewMargin fy:UB_BOTTOM],
                                                          [self.ubo fw:UB_WIDTH uw:-2*kSizeMallViewMargin-kHZGoodsDetailPicHeadViewCommonLabelHeight uh:kHZGoodsDetailPicHeadViewCommonLabelHeight]);
    [_titleLabel labelTextAlignTop];
    
    [self.picCarslView showPages:model.productPictures imageKey:APIKEY_mall_pic titleKey:@""];
    
    self.titleLabel.text = model.productTitle;
    if ([self.model.productType isEqualToString:@"gift"]) {
        self.praceLabel.text = @"赠品";
    }else{
        self.praceLabel.text = [NSString stringWithFormat:@"￥%0.2f", [model.salePrice floatValue]];
    }
    
    //价格
    _praceLabel.frame = CGRectOf([self.titleLabel.ufo fx:UB_LEFT fy:UB_BOTTOM],
                                 [self.titleLabel.ubo uw:kHZGoodsDetailPicHeadViewPriceLabelWidth uh:kHZGoodsDetailPicHeadViewCommonLabelHeight]);
    
    [_praceLabel sizeToFit];
    
//    if ([model.isNetPay isEqualToString:@"1"]) {
//        _netPayLabel.hidden = NO;
//    }else{
//        _netPayLabel.hidden = YES;
//    }
//    _netPayLabel.hidden = NO;

    CGFloat promotionLabelX = 0;
    
    if (model.promotion.count) {
        for (int i = 0; i < model.promotion.count; i++) {
            NSDictionary * promotionDit = model.promotion[i];
            NSString * promotionTitle = [NSString stringWithFormat:@" %@ ",[promotionDit stringForKey:@"name"]];
            CGFloat promotionLabelW = [XCommon widthForString:promotionTitle fontSize:9];
            DLog(@"%@  %f",promotionTitle,promotionLabelX);
            UILabel * promotionLabel = [[UILabel alloc] initWithFrame:CGRectMake(promotionLabelX, 0, promotionLabelW, _netPayLabel.ubo.height)];
            promotionLabel.textColor = HZ_WHITE_COLOR;
            promotionLabel.text = promotionTitle;
            promotionLabel.font = kHZFont_Tag_S;
            promotionLabel.backgroundColor = HZ_MALL_L_LIGHT_GRAY_BACKGROUND_COLOR;
            promotionLabel.layer.masksToBounds = YES;
            promotionLabel.layer.cornerRadius = [self.ubo uw:8];
            [_netPayLabel addSubview:promotionLabel];
            
            if (i == model.promotion.count - 1) {
                _netPayLabel.frame =  CGRectOf([self.praceLabel.ufo fx:UB_RIGHT ox:kSizeMallViewMargin/4 fy:UB_TOP oy:(_praceLabel.frame.size.height-kHZGoodsDetailPicHeadViewNetPayLabelHeight)/2],
                                               [self.ubo ow:kHZGoodsDetailPicHeadViewNetPayLabelWidth oh:kHZGoodsDetailPicHeadViewNetPayLabelHeight]);
            }
            
            promotionLabelX = promotionLabelX + promotionLabelW + 3;
        }
    }
    
    
    
    
    _moreInfoLabel.frame = CGRectOf([self.praceLabel.ufo fx:UB_LEFT fy:UB_BOTTOM uy:10],
                                                             [self.ubo fw:UB_WIDTH uw:-2*kSizeMallViewMargin oh:kHZGoodsDetailPicHeadViewNetPayLabelHeight]);
    
    [self.moreInfoLabel setText:[NSString stringWithFormat:@" 营业时间: %@    %@", model.openTime, model.shopName]];
    
    NSString *originStrG = self.moreInfoLabel.text;
    NSTextAttachment *textAttachmentG = [[NSTextAttachment alloc] init];
    textAttachmentG.image = [UIImage imageNamed:kImageClockTime];  //设置图片源
    textAttachmentG.bounds = CGRectOf([self.ubo fx:UB_LEFT fy:UB_TOP py:-3],
                                      [self.ubo ow:11 oh:11]);          //设置图片位置和大小
    NSAttributedString *attrStrAttG = [NSAttributedString attributedStringWithAttachment:textAttachmentG];
    
    
    NSMutableAttributedString *attrStrG = [[NSMutableAttributedString alloc] initWithString:originStrG];
    
    [attrStrG addAttribute:NSFontAttributeName value:kHZFont_Tag_M range: NSMakeRange(0, originStrG.length)];
    [attrStrG addAttribute:NSForegroundColorAttributeName value:HZ_MALL_TEXT_M_LIGHTGRAY_COLOR range: NSMakeRange(0, originStrG.length)];
    
    
    [attrStrG insertAttributedString:attrStrAttG atIndex:0];  //NSTextAttachment占用一个字符长度，插入后原字符串长度增加1
    
    _moreInfoLabel.attributedText = attrStrG;
    
}


@end
